package com.hemaapp.yjnh.model;

import android.content.Context;
import android.text.TextUtils;

import com.android.uu.utils.uuUtils;
import com.hemaapp.BaseConfig;
import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.HemaNetWorker;
import com.hemaapp.hm_FrameWork.result.HemaArrayResult;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.hemaapp.hm_FrameWork.result.HemaPageArrayResult;
import com.hemaapp.yjnh.BaseHttpInformation;
import com.hemaapp.yjnh.BaseNetTaskExecuteListener;
import com.hemaapp.yjnh.BaseNetWorker;
import com.hemaapp.yjnh.bean.Address;
import com.hemaapp.yjnh.bean.AlipayTrade;
import com.hemaapp.yjnh.bean.ClientInfor;
import com.hemaapp.yjnh.bean.SubOrder;
import com.hemaapp.yjnh.bean.UnionTrade;
import com.hemaapp.yjnh.bean.User;
import com.hemaapp.yjnh.bean.WeixinTrade;
import com.hemaapp.yjnh.present.BuyConfirmPresent;
import com.unionpay.UPPayAssistEx;
import com.unionpay.uppay.PayActivity;

/**
 * 类描述：
 * 创建人：GuiyouZhang
 * 创建时间：2016/9/2 14:45
 * 修改备注：
 */
public class BuyConfirmTask extends BaseNetTaskExecuteListener {

    BaseNetWorker netWorker;
    User user;
    BuyConfirmPresent present;

    public BuyConfirmTask(Context context, User user, BuyConfirmPresent present) {
        super(context);
        this.user = user;
        this.present = present;
        netWorker = new BaseNetWorker(context);
        netWorker.setOnTaskExecuteListener(this);
    }

    public void getClientInfo() {
        netWorker.clientInfor(user.getToken());
    }

    public void getAddressList() {
        netWorker.addressList(user.getToken());
    }

    public void getTotalFreight(String blog_ids, String spec_ids, String count, String address_id) {
        netWorker.billExpressGet(user.getToken(), blog_ids, spec_ids, count, address_id);
    }

    public void submitOrder(String keytype, String keyid, String specid, String addition_ids,
                            String address_id, String count, String contact_name, String contact_tel,
                            String startdate) {
        netWorker.orderAdd(user.getToken(), keytype, keyid, specid, addition_ids, address_id, count + "",
                contact_name, contact_tel, "无", startdate, "无");
    }
    public void scoreRemove(String billIds, String scores, String password) {
        netWorker.scoreRemove(user.getToken(), "1", billIds, scores, password);
    }
    //余额
    public void feeaccountRemove(String billIds, String cash, String score, String password) {
        netWorker.feeaccountRemove(user.getToken(), "1", billIds, password, cash, score);
    }
    // 支付宝
    public void alipayTrade(String billIds, String cash, String score, String password) {
        netWorker.alipay(user.getToken(), "1", "2", billIds, cash, score, password);
    }

    // 银联
    public void uppayTrade(String billIds, String cash, String score, String password) {
        netWorker.unionpay(user.getToken(), "2", "2", billIds, cash, score, password);
    }

    // 微信
    public void weixinTrade(String billIds, String cash, String score, String password) {
        netWorker.weixinpay(user.getToken(), "3", "2", billIds, cash, score, password);
    }
    //朋友代付
    public void payByFriend(String billIds, String friendNum) {
        netWorker.orderSaveoperate(user.getToken(),
                billIds+","+friendNum, "7", "无", "无", "无", "无");
    }
    public void saveInvoice(String billIds, String type, String head, String fee, String email, String content) {
        if (TextUtils.isEmpty(email))
            email = "无";
        netWorker.saveInvoice(user.getToken(), "0", billIds, type, head, fee, email, content);
    }
    @Override
    public void onPreExecute(HemaNetWorker hemaNetWorker, HemaNetTask hemaNetTask) {
        present.beforeLoad();
    }

    @Override
    public void onPostExecute(HemaNetWorker hemaNetWorker, HemaNetTask hemaNetTask) {
        present.loadFinish();
    }

    @Override
    public void onServerSuccess(HemaNetWorker hemaNetWorker, HemaNetTask hemaNetTask, HemaBaseResult hemaBaseResult) {
        BaseHttpInformation info = (BaseHttpInformation) hemaNetTask
                .getHttpInformation();
        switch (info) {
            case ACCOUNT_GET:
                HemaArrayResult<ClientInfor> cResult = (HemaArrayResult<ClientInfor>) hemaBaseResult;
                present.setClientInfor(cResult.getObjects().get(0));
                break;
            case ADDRESS_LIST:
                HemaPageArrayResult<Address> adResult = (HemaPageArrayResult<Address>) hemaBaseResult;
                if (adResult == null || adResult.getObjects() == null || adResult.getObjects().size() == 0) {
                    present.setAddress(null);
                    return;
                }
                present.setAddress(adResult.getObjects().get(0));
                break;
            case BILL_EXPRESSFEE_GET:
                HemaArrayResult<String> sResult1 = (HemaArrayResult<String>) hemaBaseResult;
                present.setFreight(sResult1.getObjects().get(0));
                break;
            case ORDER_ADD:
                HemaArrayResult<SubOrder> sResult = (HemaArrayResult<SubOrder>) hemaBaseResult;
                present.onOrderSumbitSuccess(sResult.getObjects().get(0));
                break;
            case ALIPAY:
                HemaArrayResult<AlipayTrade> aResult = (HemaArrayResult<AlipayTrade>) hemaBaseResult;
                AlipayTrade trade = aResult.getObjects().get(0);
                String orderInfo = trade.getAlipaysign();
                present.onAlipayPay(orderInfo);
                break;
            case UNIONPAY:
                HemaArrayResult<UnionTrade> uResult = (HemaArrayResult<UnionTrade>) hemaBaseResult;
                UnionTrade uTrade = uResult.getObjects().get(0);
                String uInfo = uTrade.getTn();
                present.onUniPay(uInfo);
                break;
            case WEIXINPAY:
                HemaArrayResult<WeixinTrade> wResult = (HemaArrayResult<WeixinTrade>) hemaBaseResult;
                WeixinTrade trade1 = wResult.getObjects().get(0);
                present.onWechatPay(trade1);
                break;
            case INVOICE_SAVE:
                present.onInvoiceSaveFinish("发票保存成功");
                break;
            case ORDER_SAVEOPERATE:
            case FEEACCOUNT_REMOVE:
            case SCORE_REMOVE:
                present.onPaySuccess();
                break;
        }
    }

    @Override
    public void onServerFailed(HemaNetWorker hemaNetWorker, HemaNetTask hemaNetTask, HemaBaseResult hemaBaseResult) {
        BaseHttpInformation info = (BaseHttpInformation) hemaNetTask
                .getHttpInformation();
        switch (info) {
            case ACCOUNT_GET:
            case ADDRESS_LIST:
            case BILL_EXPRESSFEE_GET:
                present.onNetFailed("获取信息失败,"+hemaBaseResult.getMsg());
                break;
            case ORDER_ADD:
                present.onNetFailed("订单提交失败,"+hemaBaseResult.getMsg());
                break;
            case INVOICE_SAVE:
                present.onInvoiceSaveFinish("发票保存失败");
                break;
            case ORDER_SAVEOPERATE:
            case ALIPAY:
            case UNIONPAY:
            case WEIXINPAY:
            case FEEACCOUNT_REMOVE:
            case SCORE_REMOVE:
                present.onPayFailed(hemaBaseResult.getMsg());
                break;
        }
    }

    @Override
    public void onExecuteFailed(HemaNetWorker hemaNetWorker, HemaNetTask hemaNetTask, int i) {
        BaseHttpInformation info = (BaseHttpInformation) hemaNetTask
                .getHttpInformation();
        switch (info) {
            case ACCOUNT_GET:
            case ADDRESS_LIST:
            case BILL_EXPRESSFEE_GET:
                present.onNetFailed("获取信息失败,请稍后再试");
                break;
            case ORDER_ADD:
                present.onNetFailed("订单提交失败,请稍后再试");
                break;
            case INVOICE_SAVE:
                present.onInvoiceSaveFinish("发票保存失败");
                break;
            case ORDER_SAVEOPERATE:
            case ALIPAY:
            case UNIONPAY:
            case WEIXINPAY:
            case FEEACCOUNT_REMOVE:
            case SCORE_REMOVE:
                present.onPayFailed("");
                break;
        }
    }
}
